package civicinity.ist.psu.edu.rssfeed;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.view.*;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ViewFlipper;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnCancelListener;
import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.adapter.CivicinityLazyAdapter;
import civicinity.ist.psu.edu.adapter.CivicinityRssViewAdapter;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.databases.CivicinityDatabase;
import civicinity.ist.psu.edu.share.CivicinityShareListview;
import civicinity.ist.psu.edu.utils.CivicinityCameraMode;
import civicinity.ist.psu.edu.utils.CivicinityDrawableManager;
import civicinity.ist.psu.edu.utils.CivicinityHelpers;

public class RSSReader extends Activity implements OnItemClickListener{
	private ArrayList<RSSDBItem> mArrRSS = null;
	private ArrayList<RSSItem> mArrItem = null;
	private String[] mArrTitle = {"Breaking News", "Community News", "PSU News"};
	private CivicinityDatabase mDB;
	
	private SharedPreferences mPref;
	private RSSFeed[] mFeed = null;
	private ArrayList<RSSDBItem> mArr = null;
	
	GestureDetector mGestureDetector;
	View.OnTouchListener mGestureListener;
	
	private ViewFlipper mFlipper;
	private Animation mSlideLeftIn;
	private Animation mSlideLeftOut;
	private Animation mSlideRightIn;
    private Animation mSlideRightOut;

    private int mRSSID = 0;
    private int mRSSType = 0;
    
    private final static int RSSTYPE_BASE = 0;
    private final static int RSSTYPE_ALL = 1;
    private final static int RSSTYPE_CATEGORY = 2;
    
    private ProgressDialog mProgress;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.rss_feed_viewflipper);
        
        // connect to the DB helper
        mDB = new CivicinityDatabase(this);
        // open the sharedpreferences
        mPref = PreferenceManager.getDefaultSharedPreferences(this);

        // initialize progress dialog
        mProgress = new ProgressDialog(this);
        
        // set mRSSType to RSSTYPE_BASE first when open this activity.
        mRSSType = RSSTYPE_BASE;
        
        // TODO: check if the user launches the app for the first time.
        if(mPref.getBoolean("first_time", true)) {
        	// get the information of the feeds from the database
            // and prepare to download the contents of the feed.
            mArr = mDB.getFeeds();
            // create RSSFeed array
            mFeed = new RSSFeed[mArr.size()];

			//Looper.prepare();
            
            // run the asynctask
    		new getFeedThread().execute(Constants.MAIN_RSSContents);
    		
    		// open the editor
    		SharedPreferences.Editor editor = mPref.edit();
    		editor.putBoolean("first_time", false);
    		editor.commit();
        }
        else {
            // retrieve data from the database.
        	viewBaseRss();
        }
    }
    
    /**
     * Retrieving the RSS as a AsyncTask
     * @author kyunghan
     *
     */
	private class getFeedThread extends AsyncTask<Integer, Void, Void> {
		
		private ProgressDialog mDialog;
		private int mFlag;
		
		@Override
	    protected void onPreExecute() {
	
			mDialog = new ProgressDialog(RSSReader.this);
			mDialog.setTitle("Downloading...");
			mDialog.setMessage("Downloading feeds now. Please wait...");
			mDialog.setCancelable(true);
			mDialog.setOnCancelListener(new OnCancelListener() {

				@Override
				public void onCancel(DialogInterface arg0) {
					// TODO Auto-generated method stub
					mDialog.dismiss();
					Toast.makeText(RSSReader.this, "Cancelled by the user.", Toast.LENGTH_SHORT).show();
					finish();
				}
	    	});
			mDialog.show();
		}

	    /** The system calls this to perform work in a worker thread and
	      * delivers it the parameters given to AsyncTask.execute() */
		@Override
	    protected Void doInBackground(Integer... flag) {
	    	
			mFlag = flag[0];
			RSSFeedHandler feedHandler = new RSSFeedHandler();
			
			// if it is first time to access this code,
			// search all feeds and refresh all items.
			if(mFlag == Constants.MAIN_RSSContents) {
				for(int i = 0 ; i < mArr.size() ; i++) {
					mFeed[i] = feedHandler.getFeed(mArr.get(i).getRSSUrl());
					Log.i("K", "Feed url : " + mArr.get(i).getRSSUrl());
				}
				
				ArrayList<RSSItem> arr = new ArrayList<RSSItem>();
				CivicinityHelpers cHelper = new CivicinityHelpers();
		    	
		    	for(int i = 0 ; i < mArr.size() ; i++) {
		    		try {
		    			if(mFeed[i] == null) {
		    				continue;
		    			}
		    			
		    			for(int j = 0 ; j < mFeed[i].getAllItems().size() ; j++) {
			    			RSSItem item = new RSSItem();
			    			item.setRSSID(i+1);
			    			item.setTitle(mFeed[i].getItem(j).getTitle().trim());
			    			item.setCategory(mFeed[i].getItem(j).getCategory().trim());
			    			item.setDescription(mFeed[i].getItem(j).getDescription().trim());
			    			item.setPubDate(cHelper.convertDateFormatRSS(mFeed[i].getItem(j).getPubDate().trim()));
			    			item.setLink(mFeed[i].getItem(j).getLink().trim());
			    			
			    			arr.add(item);
			    			// send RSSItem and RSSID
			    		}
		    		}
		    		catch(Exception e) {
		    			e.printStackTrace();
		    			Log.i("K", "Errors while fetching the news");
		    			return null;
		    		}
		    	}
		    	
		    	mDB.setRSSContents(arr, Constants.MAIN_RSSContents);	
			}
	    	else {
				// if not, search only the first one. (CDT news)
				// only updates the CDT one.
				// even if we do this, since every news will be updated in one-hour period,
				// and most of the feeds are not updated in one-hour period.
				// therefore, it is okay.
				Log.i("K", "RSS URL : " + mArr.get(0).getRSSUrl());
				try {
					RSSFeed cdtFeed = new RSSFeed();
					cdtFeed = feedHandler.getFeed(mArr.get(0).getRSSUrl());
					ArrayList<RSSItem> arr = new ArrayList<RSSItem>();
					CivicinityHelpers cHelper = new CivicinityHelpers();
					
					for(int i = 0 ; i < cdtFeed.getAllItems().size() ; i++) {
						RSSItem item = new RSSItem();
		    			item.setRSSID(i+1);
		    			item.setTitle(cdtFeed.getItem(i).getTitle().trim());
		    			item.setCategory(cdtFeed.getItem(i).getCategory().trim());
		    			item.setDescription(cdtFeed.getItem(i).getDescription().trim());
		    			item.setPubDate(cHelper.convertDateFormatRSS(cdtFeed.getItem(i).getPubDate().trim()));
		    			item.setLink(cdtFeed.getItem(i).getLink().trim());
		    			
		    			arr.add(item);
					}
					
					// pass arr values and CDT rss_id
					mDB.updateRSSContents(arr, Constants.TEMP_RSSContents);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
	    	
			return null;
	    }

	    
	    /** The system calls this to perform work in the UI thread and delivers
	      * the result from doInBackground() */
	    @Override
	    protected void onPostExecute(Void unused) {
	    	
	    	if(mDialog.isShowing()) {
	    		mDialog.dismiss();
	    	}
	    	
	    	if(mFlag == Constants.MAIN_RSSContents) {
	    		// launch the service
	    		viewBaseRss();
		        // Toast.makeText(RSSReader.this, "Start service", Toast.LENGTH_SHORT).show();
		    	startService(new Intent(RSSReader.this, RSSFeedService.class));
	    	}
	    	else {
	    		if(mRSSType == RSSTYPE_BASE) {
	    			viewBaseRss();
	    		}
	    		else if(mRSSType == RSSTYPE_ALL) {
	    			showAllFeeds();
	    		}
	    	}
	    }
	}

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    	super.dispatchTouchEvent(event);
    	return mGestureDetector.onTouchEvent(event);
    }
	
	private void viewBaseRss() {

		//showDialog(0);
		
		mRSSType = RSSTYPE_BASE;
		
		// initialize animation values.
		mSlideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
		mSlideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
		mSlideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
		mSlideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

		mFlipper = (ViewFlipper)this.findViewById(R.id.flipper);
		
		mArrRSS = new ArrayList<RSSDBItem>();
        mArrRSS = mDB.getFeeds();
        
        mRSSID = 0;
        
        //create three categories of ArrayList<RSSItem>
        //1. breaking news (CDT)
        //2. community news (citizen, gov, org, media, biz)
        //3. penn state news (edu)
        for(int i = 0 ; i < 3 ; i++) {
        	
        	mArrItem = new ArrayList<RSSItem>();
        	mArrItem = mDB.getRSSAllContents(i);
        	
        	Log.i("K", "size : " + mArrItem.size());
        	
        	LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = inflater.inflate(R.layout.rss_reader, null);
			
			// set the RSS title
			ImageView vLeftNav = (ImageView) v.findViewById(R.id.feed_title_left);
			ImageView vRightNav = (ImageView) v.findViewById(R.id.feed_title_right);
			TextView vTitle = (TextView) v.findViewById(R.id.feed_title);
			vTitle.setText(mArrTitle[i]);
			
			if(i == 0) {
				vLeftNav.setVisibility(View.GONE);
			}
			else if(i == 2) {
				vRightNav.setVisibility(View.GONE);
			}

			vLeftNav.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// if the id is 0, no need to flip, so return.
	            	if(mRSSID == 0) {
	            	}
	            	else {
	            		mRSSID--;
		            	
		            	// prepare the previous RSS contents
		            	mArrItem = mDB.getRSSAllContents(mRSSID);
		            	
		            	// change the view
		            	mFlipper.setInAnimation(mSlideRightIn);
		            	mFlipper.setOutAnimation(mSlideRightOut);
		            	mFlipper.showPrevious();
	            	}
				}
			});
			
			vRightNav.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(mRSSID == 2) {
	            	}
					else {
						mRSSID++;
		            	
		            	// prepare the next RSS contents
		            	mArrItem = mDB.getRSSAllContents(mRSSID);
		            	
		            	// change the view
		            	mFlipper.setInAnimation(mSlideLeftIn);
		            	mFlipper.setOutAnimation(mSlideLeftOut);
		            	mFlipper.showNext();
					}
				}
			});
			
			Log.i("K", "Constants.RSS_ICON_URL + mArrRSS.get(i).getRSSUrl() : " + Constants.RSS_ICON_URL + mArrRSS.get(i).getRSSUrl());
			
			// set the RSS items
			ListView vList = (ListView) v.findViewById(R.id.itemlist);
			CivicinityLazyAdapter adapter = new CivicinityLazyAdapter(this, R.layout.rss_item, mArrItem);
			vList.setAdapter(adapter);
			vList.setOnItemClickListener(this);
			vList.setOnTouchListener(mGestureListener);
			vList.setSelection(0);
			
			vList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			    @Override
			    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
			        return onLongListItemClick(v,pos,id);
			    }
			});
			
			mFlipper.addView(v, i);
			
			Log.i("K", "view #" + i + " added!");
        }
                
        // initialize gesture detector
		mGestureDetector = new GestureDetector(RSSReader.this, new MyGestureDetector(this, mFlipper));
		mGestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (mGestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
		
        Toast.makeText(RSSReader.this, "Loading the news...", Toast.LENGTH_SHORT).show();
        
		// show the first added view
		mFlipper.setDisplayedChild(0);
		mArrItem = mDB.getRSSAllContents(0);
		
		//if(mProgress.isShowing())
		//	dismissDialog(0);
	}
	
	/**
	 * MyGestureDetector.
	 * @author kyunghan
	 *
	 */
	private class MyGestureDetector extends SimpleOnGestureListener {
		
		private ViewFlipper viewFlipper;
		private static final int SWIPE_MIN_DISTANCE = 120;
		private static final int SWIPE_MAX_OFF_PATH = 250;
		private static final int SWIPE_THRESHOLD_VELOCITY = 200;
		private Animation slideLeftIn;
		private Animation slideLeftOut;
		private Animation slideRightIn;
		private Animation slideRightOut;
		private Context context;
		
		public MyGestureDetector(Context context, ViewFlipper vf) {
			this.context = context;
			viewFlipper = vf;
			
			slideLeftIn = AnimationUtils.loadAnimation(context, R.anim.slide_left_in);
	        slideLeftOut = AnimationUtils.loadAnimation(context, R.anim.slide_left_out);
	        slideRightIn = AnimationUtils.loadAnimation(context, R.anim.slide_right_in);
	        slideRightOut = AnimationUtils.loadAnimation(context, R.anim.slide_right_out);
		}
			
	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	        try {
	        	
	        	
	            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
	                return false;
	            // right to left swipe
	            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	            	
	            	// if the id is maximum value, no need to flip, so return.
	            	if(mRSSType == RSSTYPE_BASE) {
	            		if(mRSSID == 2) {
		            		return false;
		            	}
		            	mRSSID++;
		            	
		            	mArrItem = mDB.getRSSAllContents(mRSSID);
	            	}
	            	else if(mRSSType == RSSTYPE_ALL) {
	            		if(mRSSID == 7) {
		            		return false;
		            	}
		            	mRSSID++;
		            	
		            	mArrItem = mDB.getRSSContents(mRSSID+1);
	            	}
	            	
	            	// change the view
	            	viewFlipper.setInAnimation(slideLeftIn);
	            	viewFlipper.setOutAnimation(slideLeftOut);
	            	viewFlipper.showNext();
	            }
	            // left to right swipe
	            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
	            	
	            	// if the id is 0, no need to flip, so return.
	            	if(mRSSID == 0) {
	            		return false;
	            	}
	            	
	            	mRSSID--;
	            	
	            	if(mRSSType == RSSTYPE_BASE) {
		            	// prepare the next RSS contents
		            	mArrItem = mDB.getRSSAllContents(mRSSID);
	            	}
	            	else if(mRSSType == RSSTYPE_ALL) {
		            	// prepare the next RSS contents
		            	mArrItem = mDB.getRSSContents(mRSSID+1);
	            	}
	            	
	            	// change the view
	            	viewFlipper.setInAnimation(slideRightIn);
	            	viewFlipper.setOutAnimation(slideRightOut);
	            	viewFlipper.showPrevious();
	            }
	        } catch (Exception e) {
	            // nothing
	        }
	        return false;
	    }
	}
	
    /**
     * LongListPress event
     * @param v
     * @param position
     * @param id
     * @return
     */
    protected boolean onLongListItemClick(View v, final int position, long id) {
        return true;
    }
    
    /**
     * When click the item for a short time.
     */
    @Override
	public void onItemClick(AdapterView parent, View v, int position, long id) {
		// TODO Auto-generated method stub
    	Intent itemintent = new Intent(RSSReader.this, ShowDescription.class);
        
    	Bundle b = new Bundle();
    	b.putInt("rssID", mArrItem.get(position).getRSSID());
    	b.putString("title", mArrItem.get(position).getTitle());
    	b.putString("description", mArrItem.get(position).getDescription());
    	b.putString("link", mArrItem.get(position).getLink());
    	b.putString("pubdate", mArrItem.get(position).getPubDate());
    	 
    	itemintent.putExtra("android.intent.extra.INTENT", b);
         
        startActivity(itemintent);
	}
    
    /**
	 * Menu option(non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		
		menu.add(0, 1, 0, getString(R.string.menu_refresh_rss)).setIcon(android.R.drawable.ic_menu_rotate);
		menu.add(0, 2, 0, getString(R.string.menu_info_rss)).setIcon(android.R.drawable.ic_menu_info_details);
		menu.add(0, 3, 0, getString(R.string.menu_filter_rss)).setIcon(android.R.drawable.ic_menu_preferences);
			
		return true;
	}
	
	/**
	 * Menu option selection handling(non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	public boolean onOptionsItemSelected(MenuItem item){

		switch(item.getItemId()){
			case 1:
				new AlertDialog.Builder(RSSReader.this)
		   		.setTitle("Refresh the news")
		   		.setMessage("Refreshing news may take some time up to 30 seconds depending on the network strength in your area.")
		   		.setPositiveButton("Refresh", new DialogInterface.OnClickListener() {
		   			public void onClick(DialogInterface dialog, int whichButton) {
						// refresh the news.
			        	// get the information of the feeds from the database
			            // and prepare to download the contents of the feed.
		   				// but only CDT will be updated if the user accesses "Refresh" function.
		   				mArr = mDB.getFeeds();
			            // run the asynctask
			    		new getFeedThread().execute(Constants.TEMP_RSSContents);
		   			}
		   		})
		   		.setNegativeButton("Back", new DialogInterface.OnClickListener() {
		   			public void onClick(DialogInterface dialog, int whichButton) {
		   			}
		   		})
		   		.show();
	    		
				return true;
			case 2:
				startActivity(new Intent(RSSReader.this, CivicinityFeedLists.class));
				return true;
			case 3:
				showFilteredRss();
				return true;
			}
			
		return false;
	}
	
	private void showFilteredRss() {
		
		final CharSequence[] choiceSeq = {"Default", "All"};
		
		new AlertDialog.Builder(RSSReader.this)
   		.setTitle("Choose the view \n(current : " + choiceSeq[mRSSType] + ")")
   		.setItems(choiceSeq, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				if(which == RSSTYPE_BASE && mRSSType != which) {
					viewBaseRss();
				}
				else if(which == RSSTYPE_ALL && mRSSType != which){
					showAllFeeds();
				}
				else if(which == RSSTYPE_CATEGORY && mRSSType != which) {
					showCategorizedFeeds();
				}
			}
		})
   		.show();

	}
	
	private void showAllFeeds() {

		//showDialog(0);
				
		// initialize the flipper view first
		mFlipper = (ViewFlipper)this.findViewById(R.id.flipper);
		mRSSID = 0;
		
		mRSSType = RSSTYPE_ALL;
		
		for(int i = 0 ; i < mArrRSS.size() ; i++) {
			mArrItem = new ArrayList<RSSItem>();
	        mArrItem = mDB.getRSSContents(i+1);
	        	
	        Log.i("K", "size : " + mArrItem.size());
	        	
	        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View v = inflater.inflate(R.layout.rss_reader, null);
				
			// set the RSS title
			TextView vTitle = (TextView) v.findViewById(R.id.feed_title);
			vTitle.setText(mArrRSS.get(i).getRSSName());
				
			// set the RSS title
			ImageView vLeftNav = (ImageView) v.findViewById(R.id.feed_title_left);
			ImageView vRightNav = (ImageView) v.findViewById(R.id.feed_title_right);
			
			if(i == 0) {
				vLeftNav.setVisibility(View.GONE);
			}
			else if(i == 7) {
				vRightNav.setVisibility(View.GONE);
			}
			
			vLeftNav.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					// if the id is 0, no need to flip, so return.
	            	if(mRSSID == 0) {
	            	}
	            	else {
	            		mRSSID--;
		            	
		            	// prepare the previous RSS contents
		            	mArrItem = mDB.getRSSContents(mRSSID+1);
		            	
		            	// change the view
		            	mFlipper.setInAnimation(mSlideRightIn);
		            	mFlipper.setOutAnimation(mSlideRightOut);
		            	mFlipper.showPrevious();
	            	}
				}
			});
			
			vRightNav.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(mRSSID == 7) {
	            	}
					else {
						mRSSID++;
		            	
		            	// prepare the next RSS contents
		            	mArrItem = mDB.getRSSContents(mRSSID+1);
		            	
		            	// change the view
		            	mFlipper.setInAnimation(mSlideLeftIn);
		            	mFlipper.setOutAnimation(mSlideLeftOut);
		            	mFlipper.showNext();
					}
				}
			});
			
			// set the RSS items
			ListView vList = (ListView) v.findViewById(R.id.itemlist);
			CivicinityLazyAdapter adapter = new CivicinityLazyAdapter(this, R.layout.rss_item, mArrItem);
			vList.setAdapter(adapter);
			vList.setOnItemClickListener(this);
			vList.setOnTouchListener(mGestureListener);
			vList.setSelection(0);
				
			vList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			    @Override
			    public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
			        return onLongListItemClick(v,pos,id);
			    }
			});
				
			mFlipper.addView(v, i);
			
			Log.i("K", "view #" + i + " added!");
		}
		
		Toast.makeText(RSSReader.this, "Loading the news...", Toast.LENGTH_SHORT).show();
		
		// show the first added view
		mFlipper.setDisplayedChild(0);
		mArrItem = mDB.getRSSContents(1);
		
		//if(mProgress.isShowing())
		//	dismissDialog(0);
	}
	
	private void showCategorizedFeeds() {

		
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		
		if(Looper.myLooper() == null)
			Looper.prepare();
		
		switch(id) {
		case 0:
			mProgress.setTitle("Loading");
			mProgress.setMessage("Loading the news. Please wait...:)");
			mProgress.setCancelable(true);
			
			return mProgress;
		}
		
		return null;
	}

}